// logs.ts
// const util = require('../../utils/util.js')
import { formatTime } from '../../utils/util'
import { isLoggedIn, logoutAndClear } from '../../api/auth'

Component({
  data: {
    logs: [],
  },
  lifetimes: {
    attached() {
      // 检查登录状态
      if (!isLoggedIn()) {
        wx.redirectTo({
          url: '/pages/login/login'
        })
        return
      }

      this.setData({
        logs: (wx.getStorageSync('logs') || []).map((log: string) => {
          return {
            date: formatTime(new Date(log)),
            timeStamp: log
          }
        }),
      })
    }
  },
  methods: {
    /**
     * 登出
     */
    async handleLogout() {
      wx.showModal({
        title: '提示',
        content: '确定要登出吗？',
        success: async (res) => {
          if (res.confirm) {
            try {
              await logoutAndClear()
              wx.redirectTo({
                url: '/pages/login/login'
              })
            } catch (error) {
              console.error('登出失败:', error)
            }
          }
        }
      })
    }
  }
})
